package com.xbongbong.saas.enums.subform;

import com.xbongbong.paas.enums.FieldTypeEnum;
import lombok.Getter;

import java.util.Objects;

/**
 *
 * @author 魏荣杰
 * @date 2020/8/25 09:48
 * @since
 * @version
 */
@Getter
public enum ContractPerformanceSubFormEnum {
    /**
     *
     */
    CONTRACT_ID("contractId", "num_1", FieldTypeEnum.NUM.getType()),
    IS_SIGN("isSign", "num_2", FieldTypeEnum.NUM.getType()),
    RATE("rate", "num_3", FieldTypeEnum.NUM.getType()),
    AMOUNT("amount", "num_4", FieldTypeEnum.NUM.getType()),
    SIGN_TIME("signTime", "date_1", FieldTypeEnum.DATETIME.getType()),
    USER_ID("userId", "text_1", FieldTypeEnum.TEXT.getType()),
    USER_AVATAR("userAvatar", "text_2", FieldTypeEnum.TEXT.getType()),
    CURRENCY("currency", "text_3", FieldTypeEnum.TEXT.getType()),
    USER_NAME("userName", "text_4", FieldTypeEnum.TEXT.getType()),
    ;
    /**
     * 实体attr
     */
    private final String dbAttr;
    /**
     * data内attr
     */
    private final String dataAttr;
    /**
     * 字段类型，详见
     *
     * @see com.xbongbong.paas.enums.FieldTypeEnum
     */
    private final Integer fieldType;

    ContractPerformanceSubFormEnum(String dbAttr, String dataAttr, Integer fieldType) {
        this.dbAttr = dbAttr;
        this.dataAttr = dataAttr;
        this.fieldType = fieldType;
    }


    public static String getAttrConnectData(ContractPerformanceSubFormEnum cache) {
        return "data." + cache.getDataAttr();
    }

    public static String getEsAttr(ContractPerformanceSubFormEnum cache) {
        FieldTypeEnum fieldTypeEnum = FieldTypeEnum.getFieldTypeEnum(cache.getFieldType());
        //字段类型为text，或者字段在es内的类型时text，则需要在后面拼上“.keyword”
        boolean flag = fieldTypeEnum == FieldTypeEnum.TEXT || "text".equals(fieldTypeEnum.getEsalias());
        if (flag) {
            return "data." + cache.getDataAttr() + ".keyword";
        }
        return "data." + cache.getDataAttr();
    }

    public static String getEsAttrByDbAttr(String dbAttr) {
        for (ContractPerformanceSubFormEnum value : values()) {
            if (Objects.equals(dbAttr, value.getDbAttr())) {
                return getEsAttr(value);
            }
        }
        return null;
    }

    public static String getAttrConnectDataByDbAttr(String dbAttr) {
        for (ContractPerformanceSubFormEnum value : values()) {
            if (Objects.equals(dbAttr, value.getDbAttr())) {
                return getAttrConnectData(value);
            }
        }
        return null;
    }
}
